package com.goddess.yeecolor.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.goddess.yeecolor.R;
import com.goddess.yeecolor.activity.LoginAcitivity;
import com.goddess.yeecolor.activity.MyHuoDongAcitivity;
import com.goddess.yeecolor.activity.NewsActivity;
import com.goddess.yeecolor.activity.myFragmentItemActivityAll.JoinYouyasActivity;
import com.goddess.yeecolor.activity.myFragmentItemActivityAll.MyCollectionActivity;
import com.goddess.yeecolor.activity.myFragmentItemActivityAll.WatchActivity;
import com.goddess.yeecolor.activity.setActivity;
import com.goddess.yeecolor.base.BaseFragment;
import com.goddess.yeecolor.tools.JumpIntents;
import com.goddess.yeecolor.tools.LogUtils;
import com.goddess.yeecolor.tools.MyUrl;
import com.goddess.yeecolor.tools.OkttpUtils;
import com.goddess.yeecolor.tools.SharePreceUtils;
import com.goddess.yeecolor.tools.VersionTools;
import com.goddess.yeecolor.view.CircleImageView;
import com.goddess.yeecolor.view.ImageShower;
import com.goddess.yeecolor.webview.AboutsYouYaWebView;
import com.goddess.yeecolor.webview.YouYaLevelWebView;
import com.goddess.yeecolor.webview.YouYaYoupinWebView;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;


/**
 * 我的
 */
public class MyFragment extends BaseFragment {

    @InjectView(R.id.head_portrait)
    CircleImageView headPortrait;
    @InjectView(R.id.tv_login)
    TextView tvLogin;
    @InjectView(R.id.payment_one_tv)
    TextView paymentOneTv;
    @InjectView(R.id.payment_onr_img)
    ImageView paymentOnrImg;
    @InjectView(R.id.payment_one)
    RelativeLayout paymentOne;
    @InjectView(R.id.payment_to_number)
    TextView paymentToNumber;
    @InjectView(R.id.payment_to_tv)
    TextView paymentToTv;
    @InjectView(R.id.payment_to)
    LinearLayout paymentTo;
    @InjectView(R.id.participate_number)
    TextView participateNumber;
    @InjectView(R.id.participate_tv)
    TextView participateTv;
    @InjectView(R.id.participate)
    LinearLayout participate;
    @InjectView(R.id.complete_number)
    TextView completeNumber;
    @InjectView(R.id.complete_tv)
    TextView completeTv;
    @InjectView(R.id.complete)
    LinearLayout complete;
    @InjectView(R.id.yavalue_img)
    ImageView yavalueImg;
    @InjectView(R.id.yavalue_tv)
    TextView yavalueTv;
    @InjectView(R.id.yavalue)
    RelativeLayout yavalue;
    @InjectView(R.id.join_img)
    ImageView joinImg;
    @InjectView(R.id.join_tv)
    TextView joinTv;
    @InjectView(R.id.join)
    RelativeLayout join;
    @InjectView(R.id.goddess_img)
    ImageView goddessImg;
    @InjectView(R.id.goddess_tv)
    TextView goddessTv;
    @InjectView(R.id.goddess)
    RelativeLayout goddess;
    @InjectView(R.id.mycollection_img)
    ImageView mycollectionImg;
    @InjectView(R.id.mycollection_tv)
    TextView mycollectionTv;
    @InjectView(R.id.mycollection)
    RelativeLayout mycollection;
    @InjectView(R.id.watch_img)
    ImageView watchImg;
    @InjectView(R.id.watch_tv)
    TextView watchTv;
    @InjectView(R.id.watch)
    RelativeLayout watch;
    @InjectView(R.id.adout_img)
    ImageView adoutImg;
    @InjectView(R.id.adout_tv)
    TextView adoutTv;
    @InjectView(R.id.adout)
    RelativeLayout adout;
    @InjectView(R.id.comment_img)
    ImageView commentImg;
    @InjectView(R.id.comment_tv)
    TextView commentTv;
    @InjectView(R.id.comment)
    RelativeLayout comment;
    @InjectView(R.id.img_setup)
    ImageView imgSetup;
    @InjectView(R.id.img_news)
    ImageView imgNews;
    private View mView;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mView == null) {
            mView = inflater.inflate(R.layout.fragment_my, container, false);
            ButterKnife.inject(this, mView);
            initFindView();
            Map<Object, Object> map = new HashMap<>();
            map.put("unique", SharePreceUtils.getOther("user", "unique"));
            OkttpUtils.getIntence().OkttpPost(MyUrl.HUIYUAN, map, handler, 2);
        //    initData();
            initOnClck();


        }

        return mView;
    }

    @Override
    public void initOnClck() {
        comment.setOnClickListener((view)->{
            VersionTools.getIntence().getVersionNumber(getContext());
        });
        adout.setOnClickListener(view -> {
            JumpIntents.startIntent(getActivity(),AboutsYouYaWebView.class);

        });

    }

    @Override
    public void onResume() {
        super.onResume();
        isLogin= SharePreceUtils.getBoolean("user","isLogin");
        initFindView();

    }
    boolean num=false;
    @Override
    public void initFindView() {
        if (SharePreceUtils.getBoolean("user","isLogin")==true){
            tvLogin.setText(SharePreceUtils.getOther("user","nickname"));
            LogUtils.getInstence().info(SharePreceUtils.getOther("user","thumb"));
            Glide.with(this)
                    .load(SharePreceUtils.getOther("user","thumb"))
                    .diskCacheStrategy(DiskCacheStrategy.RESULT)
                    .fitCenter()
                    .dontAnimate()
                    .placeholder(R.mipmap.pic1)
                    .error(R.mipmap.pic1)
                    .into(headPortrait);

        }
    }



    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        setData();
    }
    @Override
    public void initData() {
        setData();
    }
    private void setData(){
        if (SharePreceUtils.getBoolean("user","isLogin")){
            Map<Object,Object> map=new HashMap<>();
            map.put("unique", SharePreceUtils.getOther("user","unique"));
            OkttpUtils.getIntence().OkttpPost(MyUrl.OrdersNumber,map,handler,1);
        }

    }
    private String jsonString;
    private String state="";
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            try {
                jsonString=msg.getData().getString("result");

                JSONObject object=new JSONObject(jsonString);
                int code=object.getInt("code");
                if (code==100){
                    JSONObject data=object.getJSONObject("result");
                    if (msg.what==1){
                        paymentToNumber.setText(data.optString("pending"));
                        participateNumber.setText(data.optString("tobe"));
                        completeNumber.setText(data.optString("completed"));
                    }else if(msg.what==2){
                        state=data.optString("state");

                    }
                }
            }
            catch (Exception e){
                e.printStackTrace();
            }
        }
    };
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    //所有View点击事件
    private Bundle bundle;

    @OnClick({R.id.head_portrait, R.id.payment_one, R.id.payment_to, R.id.participate, R.id.complete, R.id.yavalue, R.id.join, R.id.goddess, R.id.mycollection, R.id.watch, R.id.adout, R.id.img_setup, R.id.img_news})
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.head_portrait://点击头像
                if (SharePreceUtils.getBoolean("user","isLogin")){
                    intent=new Intent(getActivity(),ImageShower.class);
                    getActivity().overridePendingTransition(0,0);
                 //   JumpIntents.startIntent(getActivity(),ImageShower.class);
                }
                else {
                    intent=new Intent(getActivity(),LoginAcitivity.class);
                }
                break;
            case R.id.payment_one://第一个待付款
                bundle = new Bundle();
                bundle.putString("Tags", "待付款");
                intent = new Intent(getActivity(), MyHuoDongAcitivity.class);
                intent.putExtra("bundle", bundle);
                break;
            case R.id.payment_to://第二个待付款
                bundle = new Bundle();
                bundle.putString("Tags", "待付款");
                intent = new Intent(getActivity(), MyHuoDongAcitivity.class);
                intent.putExtra("bundle", bundle);

                break;
            case R.id.participate://待参与
                bundle = new Bundle();
                bundle.putString("Tags", "待参与");
                intent = new Intent(getActivity(), MyHuoDongAcitivity.class);
                intent.putExtra("bundle", bundle);

                break;
            case R.id.complete://已完成
                bundle = new Bundle();
                bundle.putString("Tags", "已完成");
                intent = new Intent(getActivity(), MyHuoDongAcitivity.class);
                intent.putExtra("bundle", bundle);
                break;
            case R.id.yavalue://雅值
                intent = new Intent(getActivity(), YouYaLevelWebView.class);
                break;
            case R.id.join://加入优雅荟
                intent = new Intent(getActivity(), JoinYouyasActivity.class);
                intent.putExtra("state",state);
                break;
            case R.id.goddess://女神优品
                intent = new Intent(getActivity(), YouYaYoupinWebView.class);
                break;
            case R.id.mycollection://个人收藏
                intent = new Intent(getActivity(), MyCollectionActivity.class);
                break;
            case R.id.watch://观看购买课程
                intent = new Intent(getActivity(), WatchActivity.class);
                break;
            case R.id.adout://关于我们

                break;
            case R.id.img_setup://Title设置按钮
                if (SharePreceUtils.getBoolean("user","isLogin")){
                    intent=new Intent(getActivity(),setActivity.class);
                }
                else {
                    intent=new Intent(getActivity(),LoginAcitivity.class);
                }
                break;
            case R.id.img_news://Title消息按钮
                intent = new Intent(getActivity(), NewsActivity.class);
                break;
        }

        if(isLogin){
            startActivity(intent);
            getActivity().overridePendingTransition(0,0);
        }
        else {
            JumpLogin();
        }

    }

    private boolean isLogin=false;
    /**
     * 判断是否登录跳转登录界面
     */
    private void JumpLogin(){
        JumpIntents.startIntent(getActivity(),LoginAcitivity.class);

    }

}
